package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.BeatMapper;
import com.sdry.mapper.hpf.PositionMapper;
import com.sdry.model.hpf.Beat;
import com.sdry.model.hpf.Position;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.EmployeeRecord;
import com.sdry.model.llm.Team;
import com.sdry.service.hpf.BeatService;

/**
 * @ClassName: BeatServiceImpl 
 * @Description:节拍Service实现
 * @Author hpf
 * @Date 2018年9月21日 上午10:28:07 
 * @version 1.0
 */
@Service
public class BeatServiceImpl implements BeatService {

	@Resource
	private BeatMapper beatMapper;
	@Resource
	private PositionMapper positionMapper;
	
	/**
	 * 查询总数
	 * @param beat
	 * @return
	 */
	public Integer countBeat(Beat beat) {
		// TODO Auto-generated method stub
		return beatMapper.countBeat(beat);
	}

	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	public List<Beat> listPageBeat(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return beatMapper.listPageBeat(map);
	}

	/**
	 * 新增节拍
	 * @param beat
	 * @return
	 */
	public Integer insertBeat(Beat beat) {
		int isSuccess = -2;
		List<Beat> list = beatMapper.getBeatByBtime(beat.getBtime());
		if(list.size() == 0){
			int i = beatMapper.insertBeat(beat);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/**
	 * 修改节拍
	 * @param beat
	 * @return
	 */
	public Integer updateBeat(Beat beat) {
		int isSuccess = -3;
		int i = 0;
		Beat b = beatMapper.getBeatByBid(beat.getBid());
		if(b.getBtime().equals(beat.getBtime())){
			i = beatMapper.updateBeat(beat);
		}else{
			List<Beat> list = beatMapper.getBeatByBtime(beat.getBtime());
			if(list.size() == 0){
				i = beatMapper.updateBeat(beat);
			}
		}
		if(i > 0){
			isSuccess = 3;
		}
		return isSuccess;
	}

	/**
	 * 校验是否删除节拍
	 * @param str 节拍ID组成的字符串
	 * @return
	 */
	public Integer deleteBeatOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<Position> positionList = positionMapper.findPositionByBid(id);
			if(positionList.size() > 0){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess;
	}
	
	/**
	 * 删除节拍
	 * @param str 节拍ID组成的字符串
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer deleteBeat(String str) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			String[] idArr = str.split(",");
			for (int i = 0; i < idArr.length; i++) {
				isSuccess = beatMapper.deleteBeat(Long.parseLong(idArr[i]));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}

	/**
	 * 查询所有
	 * @param beat
	 * @return
	 */
	public List<Beat> listAllBeat(Beat beat) {
		// TODO Auto-generated method stub
		return beatMapper.listAllBeat(beat);
	}

}
